package com.seeyon.apps.ebs.rc.controller;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.ebs.rc.manager.RcManager;
import com.seeyon.apps.ebs.rc.utils.RcUtils;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.authenticate.domain.User;
import com.seeyon.ctp.common.controller.BaseController;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class RcController extends BaseController {
    private static Log log = LogFactory.getLog(RcController.class);
    private static RcManager rcManager = (RcManager) AppContext.getBean("ebsRcManager");

    /**
    * 功能:领导日程
    * @Date: 2025/1/24
    */
    public void leadershipAgenda(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try (PrintWriter out = response.getWriter()){
            String year = request.getParameter("year");
            String userId = request.getParameter("userId");
            JSONObject rcJson = new JSONObject();
            if (year != null && !year.isEmpty() && userId != null && !userId.isEmpty()){
                rcJson.put("year",year);
                rcJson.put("userId",userId);
                JSONObject jsonObject = rcManager.leadershipAgenda(rcJson);
                out.write(jsonObject.toString());
                log.info("领导日程");
            }else{
                log.info("未接收到参数！！！");
            }
        }catch (Exception e){
            log.error("领导日程异常！！！", e);
        }
    }

    public void personalSchedule(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try (PrintWriter out = response.getWriter()){
            String year = request.getParameter("year");
            String userId = request.getParameter("userId");
            JSONObject rcJson = new JSONObject();
            if (year != null && !year.isEmpty() && userId != null && !userId.isEmpty()){
                rcJson.put("year",year);
                rcJson.put("userId",userId);
                JSONObject jsonObject = rcManager.personalSchedule(rcJson);
                out.write(jsonObject.toString());
                log.info("个人日程");
            }else{
                log.info("参数未接收到！！！");
            }
        }catch (Exception e){
            log.error("个人日程异常！！！", e);
        }
    }

    public void employeeScheduleColumn(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try (PrintWriter out = response.getWriter()){
            String userId = request.getParameter("userId");
            String year = request.getParameter("year");
            String month = request.getParameter("month");
            JSONObject dataParam = new JSONObject();
            if (userId != null && !userId.isEmpty() && year != null && !year.isEmpty() && month != null && !month.isEmpty()){
                dataParam.put("year", year);
                dataParam.put("userId", userId);
                dataParam.put("month",month);
                JSONObject jsonObject = rcManager.employeeScheduleColumn(dataParam);
                log.info("员工月度数据："+jsonObject.toString());
                out.write(jsonObject.toString());
            }
            log.info("员工日程栏目");
        }catch (Exception e){
            log.error("员工栏目异常！！！", e);
        }
    }

    public void leadershipAgendaColumn(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try (PrintWriter out = response.getWriter()){
            JSONObject dataParam = new JSONObject();
            JSONObject jsonObject = rcManager.leadershipAgendaColumn(dataParam);
            out.write(jsonObject.toString());
            log.info("领导日程栏目");
        }catch (Exception e){
            log.error("领导栏目异常！！！", e);
        }
    }

    /**
    * 功能:领导日程页面-左侧部门信息
    * @Date: 2025/2/10
    */
    public void leadershipAgendaDepartment(HttpServletRequest request, HttpServletResponse response) {
         User currentUser = AppContext.getCurrentUser();
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try (PrintWriter out = response.getWriter()){
            String year = request.getParameter("year");
            String userId = request.getParameter("userId");
            JSONObject rcJson = new JSONObject();
            if (year != null && !year.isEmpty() && userId != null && !userId.isEmpty()){
                rcJson.put("year", year);
                rcJson.put("userId", userId);
                JSONObject jsonObject = rcManager.leadershipAgendaDepartment(rcJson);
                out.write(jsonObject.toString());
            }
        }catch (Exception e){
            log.error("领导日程异常！！！", e);
        }
    }

    /**
    * 功能:领导日程页面-右侧人员动迁信息
    * @Date: 2025/2/10
    */
    public void leadershipAgendaPerson(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try (PrintWriter out = response.getWriter()){
            String year = request.getParameter("year");
            String userId = request.getParameter("userId");
            String state = request.getParameter("state");
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            String deptId = request.getParameter("deptId");
            String name = request.getParameter("name");//模糊查询
            String sortType = request.getParameter("sortType");//排序类型 ：1：降序 2：升序 为空默认为初始排序
            String sortMonth = request.getParameter("sortMonth");//排序条件：1月：Jan ...... 年总数据：Total 为空则默认为总数据
            if (year != null && !year.isEmpty() && userId != null && !userId.isEmpty() && page != null && !page.isEmpty() && limit != null && !limit.isEmpty()){
                JSONObject rcJson = new JSONObject();
                rcJson.put("year", year);
                rcJson.put("userId", userId);
                rcJson.put("state", state);
                rcJson.put("page",page);
                rcJson.put("limit",limit);
                if (sortType != null && !sortType.isEmpty() && sortMonth != null && !sortMonth.isEmpty()){
                    rcJson.put("sortType",sortType);
                    rcJson.put("sortMonth",sortMonth);
                }/*else{
                    rcJson.put("sortType",1);
                    rcJson.put("sortMonth","Total");
                }*/
                if ((deptId == null || deptId.isEmpty()) && (name == null || name.isEmpty())){
                    JSONObject jsonObject = rcManager.leadershipAgendaPerson(rcJson);
                    out.write(jsonObject.toString());
                    //点击栏目默认
                }else if((deptId != null && !deptId.isEmpty()) && (name == null || name.isEmpty())){
                    rcJson.put("deptId",deptId);
                    JSONObject jsonObject = rcManager.handleDepartmentClick(rcJson);
                    out.write(jsonObject.toString());
                    //点击部门
                }else if(name != null && !name.isEmpty()){
                    rcJson.put("name",name);
                    //模糊查询
                    if (deptId == null || deptId.isEmpty()){
                        rcJson.put("ifDepCx",false);
                        //总的查询
                    }else if (deptId != null && !deptId.isEmpty()){
                        rcJson.put("deptId",deptId);
                        //部门查询
                        rcJson.put("ifDepCx",true);
                    }
                    JSONObject jsonObject = rcManager.fuzzySearchRelocationInfo(rcJson);
                    out.write(jsonObject.toString());
                }
            }
        }catch (Exception e){
            log.error("领导日程异常！！！", e);
        }
    }

    /**
    * 功能:根据编号刷新-日程档案中的发起时间
    * @Date: 2025/1/24
    */
    public void refreshSchedule(HttpServletRequest request, HttpServletResponse response) {
        try {
            RcUtils.refreshSchedule();
            log.info("刷新成功！！！");
        }catch (Exception e){
            log.error("刷新异常！！！", e);
        }
    }
}
